<?php
namespace App\Traits\Response;

use Symfony\Component\HttpFoundation\Response;
use Illuminate\Pagination\LengthAwarePaginator;

/**
 * Traits InterfaceResponse
 * @package App\Traits\Response
 * @author zx
 * @date 2018-11-09
 */
trait InterfaceResponse{

    /**
     * 定义默认响应状态码
     *
     * @var int
     * @author zx
     * @date 2018-11-09
     */
    protected $_statusCode = Response::HTTP_OK;

    /**
     * 设置响应状态码
     *
     * @author zx
     * @param int $statusCode 响应状态码
     * @date 2018-11-09
     * @return $this
     */
    public function setStatusCode(int $statusCode)
    {
        $this->_statusCode = $statusCode;
        return $this;
    }

    /**
     * 创建资源成功响应
     *
     * @param array $data 参数
     * @param string $message 消息
     * @author zx
     * @date 2018-11-09
     * @return \Illuminate\Http\JsonResponse
     */
    public function stored(array $data=[], string $message='创建成功')
    {
        return $this->setStatusCode(Response::HTTP_CREATED)->respond($data, $message);
    }

    /**
     * 更新资源成功响应
     *
     * @param array $data 参数
     * @param string $message 消息
     * @author zx
     * @date 2018-11-09
     * @return \Illuminate\Http\JsonResponse
     */
    public function updated(array $data=[], string $message='更新成功')
    {
        return $this->setStatusCode(Response::HTTP_OK)->respond($data, $message);
    }

    /**
     * 删除资源成功响应
     *
     * @param string $message 消息
     * @author zx
     * @date 2018-11-09
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleted(string $message)
    {
        return $this->setStatusCode(Response::HTTP_NO_CONTENT)->respond([], $message);
    }

    /**
     * 异步任务响应
     *
     * @param string $message 消息
     * @author zx
     * @date 2018-11-09
     * @return \Illuminate\Http\JsonResponse
     */
    public function accepted(string $message='请求已接收,正在等待响应')
    {
        return $this->setStatusCode(Response::HTTP_ACCEPTED)->message($message);
    }

    /**
     * 资源不存在
     *
     * @param string $message 消息
     * @author zx
     * @date 2018-11-09
     * @return \Illuminate\Http\JsonResponse
     */
    public function notFound(string $message="您访问的资源不存在")
    {
        return $this->failed($message, Response::HTTP_NOT_FOUND);
    }

    /**
     * 服务器未知错误
     *
     * @param string $message 消息
     * @author zx
     * @date 2018-11-09
     * @return \Illuminate\Http\JsonResponse
     */
    public function internalError(string $message="未知错误导致请求失败")
    {
        return $this->failed($message, Response::HTTP_INTERNAL_SERVER_ERROR);
    }

    /**
     * 未知错误响应
     *
     * @param string $message 消息
     * @param int $code 状态码
     * @author zx
     * @date 2018-11-09
     * @return \Illuminate\Http\JsonResponse
     */
    public function failed(string $message, int $code=Response::HTTP_BAD_REQUEST)
    {
        return $this->message($message, $code);
    }

    /**
     * 响应消息
     *
     * @param string $message 消息
     * @param int $code 响应码
     * @author zx
     * @date 2018-11-09
     * @return \Illuminate\Http\JsonResponse
     */
    public function message(string $message, int $code=Response::HTTP_OK)
    {
        return $this->setStatusCode($code)->respond([], $message);
    }

    /**
     * 生成响应
     *
     * @author zx
     * @param array $data 参数
     * @param string $message 消息
     * @param array $headers 请求头
     * @date 2018-11-09
     * @return \Illuminate\Http\JsonResponse
     */
    public function respond(array $data=[], string $message='请求成功', array $headers=[])
    {
        return response()->json([
            'code' => $this->_statusCode,
            'message' => $message,
            'data' => $data
        ], $this->_statusCode, $headers);
    }
}